#include "Test.h"

//void Stack::Init(int n)
//{
//	int* tmp = (int*)malloc(sizeof(int) * n);
//	if (nullptr == tmp)
//	{
//		perror("malloc fail");
//		return;
//	}
//
//	_array = tmp;
//	_capacity = n;
//	_top = 0;
//}

//void Date::Init(int year, int month, int day)
//{
//	_year = year;
//	_month = month;
//	_day = day;
//
//	//this = nullptr;
//}
//
//void Date::Print()
//{
//	cout << _year << "/" << _month << "/" << _day << endl;
//	//cout << this->_year << "/" << this->_month << "/" << this->_day << endl;
//}

void Stack::Init(int n)
{
	STDataType* tmp = (STDataType*)malloc(sizeof(STDataType) * n);
	if (nullptr == tmp)
	{
		perror("malloc fail");
		return;
	}

	_array = tmp;
	_capacity = n;
	_top = 0;
}

void Stack::Push(STDataType x)
{
	if (_top == _capacity)
	{
		int newcapacity = _capacity*2;
		STDataType* tmp = (STDataType*)realloc(_array, sizeof(STDataType) * newcapacity);
		if (nullptr == tmp)
		{
			perror("malloc fail");
			return;
		}

		_array = tmp;
		_capacity = newcapacity;
	}

	_array[_top++] = x;
}

bool Stack::Empty()
{
	return _top == 0;
}

void Stack::Pop()
{
	assert(_top > 0);

	--_top;
}

STDataType Stack::Top()
{
	assert(_top > 0);

	return _array[_top - 1];
}

void Stack::Destroy()
{
	free(_array);
	_array = nullptr;

	_top = _capacity = 0;
}